package jdos.hardware;


import java.util.Arrays;

final public class RAM {
    public static int[] direct = new int[(1024*1024*4) >> 2];

    static void alloc(int size) {
    }

    static void free() {
    }

    public static byte readbs(/*HostPt*/int address) {
        return (byte) (direct[(address >> 2)] >>> ((address & 0x3) << 3));
    }

    public static /*Bit8u*/short readb(/*HostPt*/int address) {
        return (short) ((direct[(address >>> 2)] >>> ((address & 0x3) << 3)) & 0xFF);
    }

    public static void writeb(/*HostPt*/int address,/*Bit8u*/ int value) {
        int off = (address & 0x3) << 3;
        int[] local = direct;
        int mask = ~(0xFF << off);
        int index = (address >>> 2);
        int val = local[index] & mask | (value & 0xFF) << off;
        local[index] = val;
    }

    public static void writew(/*HostPt*/int address,/*Bit16u*/int value) {
        int rem = (address & 0x3);
        int[] local = direct;
        int index = (address >>> 2);
        value &= 0xFFFF;
        if (rem == 3) {
            local[index] = (local[index] & 0xFFFFFF | value << 24);
            index++;
            local[index] = (local[index] & 0xFFFFFF00 | value >>> 8);
        } else {
            int off = rem << 3;
            int mask = ~(0xFFFF << off);
            local[index] = (local[index] & mask | value << off);
        }
    }

    public static void writed(/*HostPt*/int address,/*Bit32u*/int val) {
        int rem = (address & 0x3);
        if (rem == 0) {
            direct[address >>> 2] = val;
        } else {
            int index = (address >>> 2);
            int[] local = direct;
            int off = rem << 3;
            int mask = -1 << off;
            local[index] = (local[index] & ~mask) | (val << off);
            index++;
            local[index] = (local[index] & mask) | (val >>> (32 - off));
        }
    }

}
